import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';

const baseUrl = '/parts/api/v1';

const initDesc = formatMessage({
    id: 'api.message.initDesc',
    defaultMessage: '初始化界面'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询责任单位'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询责任单位详情'
});
const newOrderDesc = formatMessage({
    id: 'api.message.newOrderDesc',
    defaultMessage: '新增责任单位'
});
const modifyDesc = formatMessage({
    id: 'api.message.modifyDesc',
    defaultMessage: '修改责任单位'
});
const startDataDesc = formatMessage({
    id: 'api.message.startDataDesc',
    defaultMessage: '启用责任单位'
});
const stopDataDesc = formatMessage({
    id: 'api.message.stopDataDesc',
    defaultMessage: '停用责任单位'
});
const personnelQueryDesc = formatMessage({
    id: 'api.message.personnelQueryDesc',
    defaultMessage: '查询人员信息'
});
//初始化品牌
export const getInitData = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/brands?${queryString}`, null, dto.InitDataStruct)
        .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, initDesc));
};
// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dutyUnits?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/dutyUnits/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
//新增
export const newOrder = data => request(`${baseUrl}/dutyUnits`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, newOrderDesc));
//修改
export const modifyOrder = (id, data) => request(`${baseUrl}/dutyUnits/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyDesc));
//启用
export const startData = (id, data) => request(`${baseUrl}/dutyUnits/${id}/start`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, startDataDesc));
//停用
export const stopData = (id, data) => request(`${baseUrl}/dutyUnits/${id}/stop`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, stopDataDesc));
// 查询人员
export const personnelQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/personnels/simple?${queryString}`, null, dto.Personnel)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, personnelQueryDesc));
};
